package codetop.greedy;

import java.util.Arrays;

//分发糖果
public class Code135 {
    public int candy(int[] ratings) {
        int []sup=new int[ratings.length];
        Arrays.fill(sup,1);
        for (int i = 0; i < ratings.length-1; i++) {
            if (ratings[i+1]>ratings[i])sup[i+1]=sup[i]+1;
        }
        for (int i = ratings.length-1; i >0; i--) {
            if (ratings[i]<ratings[i-1]) {
                sup[i-1]=Math.max(sup[i-1],sup[i]+1);
            }
        }
        int sum=0;
        for (int i = 0; i < sup.length; i++) {
            sum+=sup[i];
        }
        return sum;
    }
}
